package cn.iocoder.idata.module.idata.controller.admin.dataset;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.common.pojo.CommonResult;
import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;

import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;

import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.idata.module.idata.controller.admin.dataset.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.dataset.DataSetDO;
import cn.iocoder.idata.module.idata.convert.dataset.DataSetConvert;
import cn.iocoder.idata.module.idata.service.dataset.DataSetService;

@Tag(name = "管理后台 - 数据集")
@RestController
@RequestMapping("/idata/data-set")
@Validated
public class DataSetController {

    @Resource
    private DataSetService dataSetService;

    @PostMapping("/create")
    @Operation(summary = "创建数据集")
    @PreAuthorize("@ss.hasPermission('idata:data-set:create')")
    public CommonResult<Long> createDataSet(@Valid @RequestBody DataSetCreateReqVO createReqVO) {
        return success(dataSetService.createDataSet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据集")
    @PreAuthorize("@ss.hasPermission('idata:data-set:update')")
    public CommonResult<Boolean> updateDataSet(@Valid @RequestBody DataSetUpdateReqVO updateReqVO) {
        dataSetService.updateDataSet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据集")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('idata:data-set:delete')")
    public CommonResult<Boolean> deleteDataSet(@RequestParam("id") Long id) {
        dataSetService.deleteDataSet(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('idata:data-set:query')")
    public CommonResult<DataSetRespVO> getDataSet(@RequestParam("id") Long id) {
        DataSetDO dataSet = dataSetService.getDataSet(id);
        return success(DataSetConvert.INSTANCE.convert(dataSet));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据集列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('idata:data-set:query')")
    public CommonResult<List<DataSetRespVO>> getDataSetList(@RequestParam("ids") Collection<Long> ids) {
        List<DataSetDO> list = dataSetService.getDataSetList(ids);
        return success(DataSetConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据集分页")
    @PreAuthorize("@ss.hasPermission('idata:data-set:query')")
    public CommonResult<PageResult<DataSetRespVO>> getDataSetPage(@Valid DataSetPageReqVO pageVO) {
        PageResult<DataSetDO> pageResult = dataSetService.getDataSetPage(pageVO);
        return success(DataSetConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据集 Excel")
    @PreAuthorize("@ss.hasPermission('idata:data-set:export')")
    @OperateLog(type = EXPORT)
    public void exportDataSetExcel(@Valid DataSetExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DataSetDO> list = dataSetService.getDataSetList(exportReqVO);
        // 导出 Excel
        List<DataSetExcelVO> datas = DataSetConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据集.xls", "数据", DataSetExcelVO.class, datas);
    }

}
